//
//  SDHSportsHomeVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/3.
//

#import "SDHSportsHomeVC.h"
#import "SDHSportsInteVC.h"
#import "SDHSportsHomeNewsVC.h"
#import "SDHSportsTeamInfoVC.h"
#import "SDHSportsDataManager.h"
#import "SDHSportsNewsDetailVC.h"
#import "SDHSportsHomeNewsCell.h"
#import "SDHSportsNewsModal.h"

@interface SDHSportsHomeVC () <UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *bgImgView;
@property (nonatomic, strong) UIView *panelView;
@property (nonatomic, strong) SDHSportsHomeNewsVC *homeNewsVC;

@property (nonatomic, strong) UISearchController *searchVc;
@property (nonatomic, retain) UITableViewController *searchTVC;
@property (nonatomic, strong) NSMutableArray *searchResultNewsArray;

@end

@implementation SDHSportsHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initUI];
    [self initSearchBar];
    
    _searchResultNewsArray = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterNewsDetail:) name:@"newsDetailNoti" object:nil];
}

- (void)initUI {
    // 添加背景图片
    UIImageView *bgImgView = [UIImageView new];
    UIImage *image = [UIImage imageNamed:@"bg1"];
    bgImgView.image = image;
    [self.view addSubview:bgImgView];
    self.bgImgView = bgImgView;

    UIView *panelView = [UIView new];
    panelView.backgroundColor = [UIColor clearColor]; // test code
    [self.view addSubview:panelView];
    self.panelView = panelView;
        
    UILabel *label = [UILabel new];
    label.text = @"Want to Know the latest basketball game? come in and have a look";
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:20];
    label.tag = 101;
    [panelView addSubview:label];
        
    SDHSportsHomeNewsVC *vc = [[SDHSportsHomeNewsVC alloc] init];
    [panelView addSubview:vc.view];
    [self addChildViewController:vc];
    self.homeNewsVC = vc;
    
    UIView *superview = self.view;
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superview).insets(UIEdgeInsetsMake(20, 0, 0, 0));
        make.height.equalTo(superview.mas_height);
    }];
    [panelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superview).offset(20);
        make.left.equalTo(superview).offset(0);
        make.right.equalTo(superview).offset(0);
        make.bottom.equalTo(superview).offset(0);
    }];

    superview = panelView;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superview).offset(10);
        make.left.equalTo(superview).offset(10);
        make.right.equalTo(superview).offset(-10);
        make.height.equalTo(@100);
    }];

    [self.homeNewsVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom);
        make.left.equalTo(panelView.mas_left);
        make.right.equalTo(panelView.mas_right);
        make.bottom.equalTo(panelView).offset(0);
    }];
}

- (void)initSearchBar {
    
    UIButton *btn = [UIButton new];
    [btn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
//    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(44);
    }];
    
    UITableViewController *tableVC = [[UITableViewController alloc] init];
    tableVC.tableView.delegate = self;
    tableVC.tableView.dataSource = self;
    tableVC.tableView.tableFooterView = [UIView new];
    self.searchTVC = tableVC;
    
    UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchTVC];
    searchController.delegate = self;
    searchController.searchResultsUpdater = self;
    self.searchVc = searchController;
    
    UISearchBar *searchBar = searchController.searchBar;
    searchBar.hidden = YES;
    [self.view addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(-40);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(kScreenWidth);
    }];
}

- (void)search:(UIButton *)btn {
    self.searchVc.searchBar.hidden = NO;
    
    UISearchBar *searchBar = self.searchVc.searchBar;
    [searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
    }];
    [searchBar becomeFirstResponder];
}

- (void)searchStr:(NSString *)str {
    [_searchResultNewsArray removeAllObjects];
    NSArray *array = [SDHSportsDataManager sharedManager].newsArray;
    for (SDHSportsNewsModal *model in array) {
        if ([model.en_title containsString:str]) {
            [_searchResultNewsArray addObject:model];
        }
    }
    
    [self.searchTVC.tableView reloadData];
}

- (void)updateSearchBar {
    self.navigationController.navigationBar.hidden = YES;
    
    UISearchBar *searchBar = self.searchVc.searchBar;
    searchBar.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)enterNewsDetail:(NSNotification *)notifier {
    SDHSportsNewsModal *model = [notifier object];
    SDHSportsNewsDetailVC *vc = [[SDHSportsNewsDetailVC alloc] init];
    vc.model = model;
    vc.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchResultNewsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsHomeNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsHomeNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    SDHSportsNewsModal *modal = [_searchResultNewsArray objectAtIndex:indexPath.row];
    [cell updateContent:modal];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SDHSportsNewsModal *model = [_searchResultNewsArray objectAtIndex:indexPath.row];

    [self.searchVc.searchBar resignFirstResponder];
    [self.searchVc resignFirstResponder];
    self.searchVc.active = NO;
    [self updateSearchBar];
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        SDHSportsNewsDetailVC *vc = [[SDHSportsNewsDetailVC alloc] init];
        vc.model = model;
        vc.hidesBottomBarWhenPushed = YES;
        self.navigationController.navigationBar.hidden = NO;
        [self.navigationController pushViewController:vc animated:YES];
//    });
    
    [[SDHSportsDataManager sharedManager] addBrowseNews:model];
}

#pragma mark ---- UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    if (!searchController.active) {
        return;
    }
    
    NSString *str = searchController.searchBar.text;
    [self searchStr:str];
}

- (void)willPresentSearchController:(UISearchController *)searchController {
    NSLog(@"willPresentSearchController 111111");
}

- (void)didPresentSearchController:(UISearchController *)searchController {
    NSLog(@"didPresentSearchController ######22222");
}

- (void)willDismissSearchController:(UISearchController *)searchController {
    NSLog(@"willDismissSearchController ######33333");
}

- (void)didDismissSearchController:(UISearchController *)searchController {
    NSLog(@"didDismissSearchController ######44444");
    [self updateSearchBar];
}

@end
